<?php

namespace app\admin\controller;
use app\common\controller\Admin;
class Adsmaterialtag extends Admin {
    /**
     * 小说对外开放接口
     *
     * @Author 小邓
     * @DateTime 2022-11-03
     * @return void
     */
	public function _initialize() {
		parent::_initialize();
        $types = [
            [ "id" => 'wenan', "name" => "文案"],
            [ "id" => 'img', "name" => "图片"],
            [ "id" => 'video', "name" => "视频"],
            [ "id" => 'bgm', "name" => "bgm"],
        ];
        $this->assign(compact('types'));
    }

    public function index(){
        $keyword = input('get.keyword');
        $map     = [];
        if($keyword){
            $map['id|title'] = ["like","%$keyword%"];
        }
        $list = db('ads_material_tag')->where($map)->paginate( 200, false, [ 'type'=> 'page\Page' ,'query' => request()->param()] );
        foreach ($list as $k => $v) {
            $list[$k] = $v;
        }
        $page = $list->render();
        $this->assign(compact('keyword', 'list', 'page'));
        $this->setMeta("账号管理");
        return view();
    }

    public function add() {
        if(IS_POST){
            $post = input('post.');
            if(isset($post['types']) && is_array($post['types'])){
                $post['types'] = implode(",", $post['types']);
            }
            if( isset($post['id']) && !$post['id'] ){
                // $post['apikey'] = md5(uniqid());
                db('ads_material_tag')->insert($post);
                return $this->success("添加成功！");
            }else{
                db('ads_material_tag')->update($post);
                return $this->success("修改成功！");
            }
            
        }
        return $this->error("失败！");
    }

    public function del() {
		$id = $this->getArrayParam('id');
		if (empty($id)) {
			return $this->error("非法操作！");
		}
		$map['id'] = array('IN', $id);
		$result    = db('ads_material_tag')->where($map)->delete();
		if (false !== $result) {
			return $this->success("删除成功！");
		} else {
			return $this->error("删除失败！");
		}
	}

    //批量换标签，以、分开
    public function test()
    {
        $tag = '扮猪吃虎、先婚后爱、逆袭、马甲、真假千金、女强、萌宝、年代、虐恋、病娇、虐渣、双强、穿越、空间、权谋、爽文、甜宠、宫斗、团宠、姐弟恋、宅斗、种田、重生宠夫、重生复仇、打脸、女扮男装、仙侠、穿书、破镜重圆、女尊、系统、追妻火葬场、沙雕、医术、兽世、忠犬男主、双重生、群穿、悬疑推理、日久生情、替身、娱乐圈、民国、商战、玄学、豪门总裁、替嫁、暗恋、带球跑、契约婚姻、古穿今、群穿、悬疑推理、青梅竹马、家庭、快穿、职场、脑洞、一妻多夫、校园';
        $arr = explode("、", $tag);
        $insertData = [];
        $sort = 77;
        $type = "wenan,img,video";
        $name = "女频";
        foreach ($arr as $tag) {
            $tag = trim($tag);
            $map = [
                "name" => $name,
                "title" => $tag,
            ];
            $id = db('ads_material_tag')->where($map)->value('id');
            $insertData[] = [
                "id" => $id?:null,
                'name' => $name,
                'title' => $tag,
                'update_time' => time(),
                'create_time' => time(),
                'types' => $type,
                'sort' => $sort
            ];
            $sort++;
        }
        db('ads_material_tag')->insertAll($insertData, true);
        echo  db('ads_material_tag')->getLastSql();
    }
}